﻿namespace OutLook.Module.Load
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using OutLook.Module.Infrastructure;
    using OutLook.Module.Load.Contracts;
    using OutLook.Module.Infrastructure.Interfaces;
    using OutLook.Model.Domains;
    using System.Data;
    using OutLook.Module.Load.Properties;
    using OutLook.Model.Infrastructure;
    using System.Globalization;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ModelController : DomainControllerBase, IModelContract
    {
        #region Constructors

        public ModelController(IRepository context)
            : base(context) { }

        #endregion

        #region Properties

        public Model CurrentModel
        {
            get { return m_CurrentModel; }
            set { m_CurrentModel = value; }
        }

        #endregion

        #region DomainControllerBase members

        protected override void Dispose(bool dispoceManged)
        {
            if (m_Disposed)
                return;

            m_Disposed = true;
        }

        #endregion

        #region IModelContract Implementation

        public double GetElementDistribution(ElementModel rule, Load load, Technology technology)
        {
            double sum = 0;

            if (rule != null)
            {
                foreach (Coupla elem in rule.DistributionMode)
                {
                    string[] propNames = elem.Indicator.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

                    if (propNames.Length > 2)
                    {
                        double val = 0;
                        string[] afterMinus = elem.Indicator.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                        propNames = afterMinus[0].Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

                        if (propNames[0].Equals("Load"))
                            val += (double)load.GetType().GetProperty(propNames[1]).GetValue(load, null);

                        if (propNames[0].Equals("Technology"))
                            val += (double)technology.GetType().GetProperty(propNames[1]).GetValue(technology, null);

                        double value;
                        bool action = double.TryParse(afterMinus[1],System.Globalization.NumberStyles.Any, CultureInfo.CreateSpecificCulture("en-EN"), out value);// Convert.ToDouble(afterMinus[1].Replace('.', ','));
                        if (action)
                            val -= value;
                        else
                        {
                            string[] afterPlus = afterMinus[1].Replace(',', '.').Split(new char[] { '+' }, StringSplitOptions.RemoveEmptyEntries);

                            action = double.TryParse(afterPlus[1],System.Globalization.NumberStyles.Any, CultureInfo.CreateSpecificCulture("en-EN"), out value);
                            if (action)
                                val += value;

                            propNames = afterPlus[0].Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
                            string[] p = propNames[1].Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                            action = double.TryParse(propNames[0],System.Globalization.NumberStyles.Any, CultureInfo.CreateSpecificCulture("en-EN"), out value);
                            if (action)
                            {
                                if (p[0].Equals("Load"))
                                    val -= value * (double)load.GetType().GetProperty(p[1]).GetValue(load, null);

                                if (p[0].Equals("Technology"))
                                    val -= value * (double)technology.GetType().GetProperty(p[1]).GetValue(technology, null);
                            }

                        }

                        sum += elem.Value * Math.Pow(val, 2);

                    }
                    else
                    {
                        if (propNames[0].Equals("Load"))
                            sum += elem.Value * (double)load.GetType().GetProperty(propNames[1]).GetValue(load, null);

                        if (propNames[0].Equals("Technology"))
                            sum += elem.Value * (double)technology.GetType().GetProperty(propNames[1]).GetValue(technology, null);

                        if (propNames[0].Equals("-"))
                            sum += elem.Value;
                    }
                }
            }

            return sum;
        }

        public double GetElementDistribution(string rule, Load load, Technology technology)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Fields

        private bool m_Disposed = false;
        private Model m_CurrentModel = null;

        #endregion
    }
}
